import React, { Component } from 'react'
import ReactDOM from 'react-dom'

export default class Class_Demo extends Component {
  state={count:0}

  myRef=React.createRef()

  add=()=>{
    this.setState(state=>({count:state.count+1}))
  }

  unmount=()=>{
    ReactDOM.unmountComponentAtNode(document.getElementById('root'))
  }
  
  show=()=>{
    alert(this.myRef.current.value)
  }

  componentDidMount(){
    this.timer=setInterval(()=>{
      this.add()
    },1000)
  }

  componentWillUnmount(){
    clearInterval(this.timer)
  }

  render() {
    const { count } = this.state
    return (
      <div>
        <h2>我是class组件</h2>
        <input type="text" ref={this.myRef}/>
        <h2>state的值为：{count}</h2>
        <button onClick={this.add}>点我+1</button>
        <button onClick={this.unmount}>卸载组件</button>
        <button onClick={this.show}>点击提示数据</button>
      </div>
    )
  }
}
